﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using LTP.Common;
namespace TCS.Web.Admin.BusinessManage
{
    public partial class ProcurementInfoStatistics : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Session["strWhereProduct"] = "";
                gridView.BorderColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_bordercolorlight"].ToString());
                gridView.HeaderStyle.BackColor = ColorTranslator.FromHtml(Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString());
              
                BindData();
            }
        }
        
        #region BindData
        private void BindData()
        {
            #region 权限检查
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[5].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            #endregion

            string dpid = new LTP.Accounts.Bus.User().GetUserdepartmentID(Context.User.Identity.Name);
            TCS.BLL.Gsp_ProcurementInfo bll = new TCS.BLL.Gsp_ProcurementInfo();
            //TCS.Model.MemberLogin mo = (TCS.Model.MemberLogin)Session["BangShangLogin_model"];
            string strWhere = "";
            if (dpid != "1")
            {
                strWhere = " (1=1) and MemberID in(select MemberID from Gsp_MemberInfo where AddID=" + dpid + ")";
            }
            else
            {
                strWhere = " (1=1) ";
            }
            if (Session["strWhereProduct"] != null && Session["strWhereProduct"].ToString() != "")
            {
                strWhere += Session["strWhereProduct"].ToString();
            }
            DataSet ds = new DataSet();

            if (strWhere == "")
            {
                ds = bll.GetStatisticsList(strWhere);
            }
            else
            {
                ds = bll.GetStatisticsList(strWhere);
            }
            DataView dv = ds.Tables[0].DefaultView;
            gridView.DataSource = dv;
            gridView.DataBind();

            //分页
            int rows_Count = ds.Tables[0].Rows.Count;
            int page_Size = gridView.PageSize;
            int page_Count = gridView.PageCount;
            int page_Current = gridView.PageIndex + 1;

            lblRowsCount.Text = rows_Count.ToString();
            lblPageCount.Text = page_Count.ToString();
            lblCurrentPage.Text = page_Current.ToString();


            #region 显示页导航

            btnFirst.Enabled = true;
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            if (gridView.PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (gridView.PageCount == 1)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (gridView.PageIndex == gridView.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }

            #endregion

        }
        #endregion

        #region 页导航事件
        public void NavigateToPage(object sender, CommandEventArgs e)
        {
            btnFirst.Enabled = true;
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
            btnLast.Enabled = true;
            string pageinfo = e.CommandArgument.ToString();
            switch (pageinfo)
            {
                case "Prev":
                    if (gridView.PageIndex > 0)
                    {
                        gridView.PageIndex -= 1;

                    }
                    break;
                case "Next":
                    if (gridView.PageIndex < (gridView.PageCount - 1))
                    {
                        gridView.PageIndex += 1;

                    }
                    break;
                case "First":
                    gridView.PageIndex = 0;
                    break;
                case "Last":
                    gridView.PageIndex = gridView.PageCount - 1;
                    break;
            }
            if (gridView.PageIndex == 0)
            {
                btnFirst.Enabled = false;
                btnPrev.Enabled = false;
                if (gridView.PageCount == 1)
                {
                    btnLast.Enabled = false;
                    btnNext.Enabled = false;
                }
            }
            else if (gridView.PageIndex == gridView.PageCount - 1)
            {
                btnLast.Enabled = false;
                btnNext.Enabled = false;
            }
            BindData();
        }
        #endregion

        #region btn_Search
        protected void btn_Search_Click(object sender, ImageClickEventArgs e)
        {
            string SupplierName = this.txtKey.Text.Trim();
            //string field = this.DropField.SelectedValue;

            string strsql = "";
            //if (SupplierName != "")
            //{
            //    strsql += " and (" + field + " like'%" + SupplierName + "%')";

            //}

            //if (strsql != "")
            //{
            //    Session["strWhereProduct"] = " (1=1) " + strsql;
            //}
            //else
            //{
            //    Session["strWhereProduct"] = "";
            //}
            string field = "";
            string bengindate = txtBugDateBegin.Text;
            string enddate = txtBugDateEnd.Text;
           
            field = "BuyTime";//购入日期
            if (bengindate != "")
            {
                if (!PageValidate.IsDateTime(bengindate))
                {
                    MessageBox.Show(this, "开始日期格式不正确");
                    return;
                }
                strsql += " and (convert(datetime," + field + ")>='" + bengindate + "' )";
            }
            if (enddate != "")
            {
                if (!PageValidate.IsDateTime(enddate))
                {
                    MessageBox.Show(this, "结束日期格式不正确");
                    return;
                }
                strsql += " and (convert(datetime," + field + ")<='" + enddate + "' )";
            }
            //if (dropProductName.SelectedValue != "")
            //{
            //    strsql += " and (DrugID=" + dropProductName.SelectedValue + ")";
            //}
            if (SupplierName != "" && SupplierName != "请输入关键字")
            {
                if (DropField.SelectedValue == "1")
                {
                    strsql += " and MemberID in ( select MemberID from Gsp_MemberInfo where EnName like'%" + SupplierName + "%')";
                }
                else if (DropField.SelectedValue == "2")
                {
                    strsql += " and DrugID in ( select DrugID from Gsp_DrugsInfo where ProductName like'%" + SupplierName + "%')";
                }
                else
                {
                    strsql += " and DrugID in ( select DrugID from Gsp_DrugsInfo where ComName like'%" + SupplierName + "%')";
                }
            }
            if (strsql != "")
            {
                Session["strWhereProduct"] = "  " + strsql;
            }
            else
            {
                Session["strWhereProduct"] = "";
            }

            BindData();
        }
        #endregion

        #region gridView事件
        protected void gridView_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //页导航
            if (e.Row.RowType == DataControlRowType.Pager)
            {
                TableRow row = e.Row.Controls[0].Controls[0].Controls[0] as TableRow;
                foreach (TableCell cell in row.Cells)
                {
                    Control lb = cell.Controls[0];
                    if (lb is Label)
                    {
                        Label lblpage = (Label)lb;
                        lblpage.ForeColor = System.Drawing.ColorTranslator.FromHtml("#e78a29");
                        lblpage.Font.Bold = true;
                        lblpage.Text = "[" + lblpage.Text + "]";
                        //((Label)lb).Font.Size = new FontUnit("40px");
                    }
                    else
                        if (lb is LinkButton)
                        {
                            LinkButton lblpage = (LinkButton)lb;
                            lblpage.Font.Bold = true;
                            lblpage.Text = "[" + lblpage.Text + "]";
                        }
                }
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string title = Application[Session["Style"].ToString() + "xtable_titlebgcolor"].ToString();
                //string bgcolor = Application[Session["Style"].ToString() + "xtable_bgcolor"].ToString();
                e.Row.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='" + title + "';this.style.cursor='hand';");
                //当鼠标移走时还原该行的背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor");
            }

        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //string CategoryId = DataBinder.Eval(e.Row.DataItem, "CategoryId").ToString();
                //if (CategoryId != "")
                //{
                //    string name = bllc.GetName(CategoryId);
                //    e.Row.Cells[2].Text = name;
                //}
                //else
                //{
                //    e.Row.Cells[2].Text = "未知";
                //}
            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //if (gridView.Rows.Count <= 1)
            //{
            //    e.Cancel = true;
            //}
            string ProductId = gridView.DataKeys[e.RowIndex].Value.ToString();
            TCS.BLL.Gsp_DrugsInfo bll = new TCS.BLL.Gsp_DrugsInfo();
            bll.Delete(int.Parse(ProductId));
            BindData();
        }

        protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
        {

            TCS.BLL.Gsp_DrugsInfo bll = new TCS.BLL.Gsp_DrugsInfo();
            string strWhere = "";
            if (Session["strWhereProduct"] != null && Session["strWhereProduct"].ToString() != "")
            {
                strWhere += Session["strWhereProduct"].ToString();
            }
            DataSet ds = new DataSet();
            ds = bll.GetList(strWhere);
            DataView dv = ds.Tables[0].DefaultView;
            if (ViewState["SortDirection"] == null)
            {
                ViewState["SortDirection"] = "desc";
            }
            else
            {
                if (ViewState["SortDirection"].ToString() == "asc")
                {
                    ViewState["SortDirection"] = "desc";
                }
                else
                {
                    ViewState["SortDirection"] = "asc";
                }
            }
            dv.Sort = e.SortExpression + " " + ViewState["SortDirection"].ToString();
            gridView.DataSource = dv;
            gridView.DataBind();


        }
        #endregion

        protected void btn_Relese_Click(object sender, EventArgs e)
        {
            TCS.BLL.Products.Product bll = new TCS.BLL.Products.Product();
            string idlist = GetSelIDlist();
            if (idlist == "")
            {
                LTP.Common.MessageBox.Show(this, "没有选择项！");
                return;
            }
            bll.UpList(idlist);
            Response.Redirect("index.aspx");

        }

        private string GetSelIDlist()
        {
            string dgIDs = "";
            bool BxsChkd = false;
            foreach (GridViewRow row in this.gridView.Rows)
            {
                CheckBox deleteChkBxItem = (CheckBox)row.FindControl("SelectThis");
                if (deleteChkBxItem.Checked)
                {
                    BxsChkd = true;
                    dgIDs += row.Cells[1].Text + ",";
                }
            }
            if (BxsChkd)
            {
                dgIDs = dgIDs.Substring(0, dgIDs.LastIndexOf(","));
            }
            return dgIDs;
        }
        protected string GetDrugName(string DrugId)
        {
            TCS.Model.Gsp_DrugsInfo model = new TCS.BLL.Gsp_DrugsInfo().GetModel(int.Parse(DrugId));
            if (model != null)
            {
                return model.ProductName;
            }
            else
            {
                return "";
            }
        }
        protected string GetDrugComeName(string DrugId)
        {
            TCS.Model.Gsp_DrugsInfo model = new TCS.BLL.Gsp_DrugsInfo().GetModel(int.Parse(DrugId));
            if (model != null)
            {
                return model.ComName;
            }
            else
            {
                return "";
            }
        }
        protected string GetEntNameByMemberID(string MemberID)
        {
            TCS.Model.MemberInfo model = new TCS.BLL.MemberInfo().GetModel(int.Parse(MemberID));
            return model.MI_username;
        }
        protected string GetNum(string n)
        {
            if ((this.gridView.PageIndex + 1) > 1)
            {
                return Convert.ToString((gridView.PageIndex) * gridView.PageSize + (int.Parse(n) + 1));
            }
            else
            {
                return Convert.ToString(int.Parse(n) + 1);
            }
        }
    }
}
